package com.dusk.util
{
    import flash.geom.Matrix;
    import flash.geom.Point;
    
    public class StringUtil extends UtilBase
    {
        public static const ELLIPSIS:String = "…";
        
        public function StringUtil()
        {
            super();
        }
        
        /**
         * 获取字符串中的第一个A-Z字符
         */
        public static function getFirstAZChar(input:String):String
        {
            var index:int = 0;
            var AZChars:Array = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"];
            var char:String = input.substr(index, index + 1).toUpperCase();
            while (AZChars.indexOf(char) == -1)
            {
                char = input.substr(index, index + 1).toUpperCase();
                index++;
            }
            return char;
        }
        
        /**
         * 将字符串的首字母大写
         */
        public static function toUpperCaseFirst(input:String):String
        {
            var firstChar:String = input.slice(0, 1).toUpperCase();
            var restOfString:String = input.slice(1);
            return firstChar + restOfString;
        }
        
        /**
         * 将字符串的首字母小写
         */
        public static function toLowerCaseFirst(input:String):String
        {
            var firstChar:String = input.slice(0, 1).toLowerCase();
            var restOfString:String = input.slice(1);
            return firstChar + restOfString;
        }
        
        /**
         * 将字符串的首字母大写，其他字符小写
         */
        public static function upperCaseFirstCharAndLowerCaseOthers(input:String):String
        {
            var firstAZChar:String = getFirstAZChar(input);
            return firstAZChar + input.substr(1, input.length).toLowerCase();
        }
        
        /**
         * 去除字符串两端的空白字符
         */
        public static function trim(input:String):String
        {
            return rtrim(ltrim(input));
        }
        
        /**
         * 去除字符串左端的空白字符
         */
        public static function ltrim(input:String):String
        {
            var leftTrimPattern:RegExp = /^\s*/;
            return input.replace(leftTrimPattern, "");
        }
        
        /**
         * 去除字符串右端的空白字符
         */
        public static function rtrim(input:String):String
        {
            var rightTrimPattern:RegExp = /\s*$/;
            return input.replace(rightTrimPattern, "");
        }
        
        /**
         * 检查是否是有效的邮箱地址
         */
        public static function isEmail(input:String):Boolean
        {
            if (input)
            {
                return trim(input).match(/\b[\w.-]+@[\w.-]+\.\w{2,4}\b/gi).length == 1;
            }
            return false;
        }
        /**
         * 检查是否是有效的手机号码
         */
        public static function isMobileNO(input:String):Boolean
        {
            if (input && input.length == 11)
            {
                return input.match(/\d{11}/).length == 1;
            }
            return false;
        }
        
        /**
         * 截断字符串并添加省略号
         * @param input 输入字符串
         * @param length 截取长度
         * @param appendEllipsis 是否添加省略号
         * @param ellipsis 省略号格式，默认为"…"
         * @return 截断后的字符串
         */
        public static function truncate(input:String, length:int, appendEllipsis:Boolean = true, ellipsis:String = ELLIPSIS):String
        {
            var truncated:String = null;
            if (input.length > length)
            {
                truncated = input.substr(0, length);
                if (appendEllipsis)
                {
                    truncated += ellipsis;
                }
                return truncated;
            }
            return input;
        }
        
        /**
         * 移除字符串中的所有换行符
         * @param input 输入字符串
         */
        public static function clearEnterString(input:String):String
        {
            if (input)
            {
                return input.replace(/\s/g, " ");
            }
            return "";
        }
        
        /**
         * 将字符串转换为Point对象
         * @param input 输入字符串
         * @return Point
         */
        public static function stringToPoint(input:String):Point
        {
            var a:Array = input.split(";");
            return new Point(a[0], a[1]);
        }
        
        // 构建矩阵对象
        private static function buildMatrixFromString(input:String):Matrix
        {
            var pairs:Array = input.split(",");
            var matrix:Matrix = new Matrix();
            for (var i:int = 0; i < pairs.length; i++)
            {
                var pair:Array = pairs[i].split("=");
                var key:String = trim(pair[0]);
                matrix[key] = pair[1];
            }
            return matrix;
        }
        
        /**
         * 替换字符串中的占位符
         * @param input 字符串
         * @param rest 替换的内容
         * @return 替换后的字符串
         * @example
         * replaceWords("{} is {}", "today", "monday")
         * result: "today is monday"
         */
        public static function replaceWords(input:String = "", ...rest):String
        {
            var placeholder:String = "{}";
            var placeholderPattern:RegExp = new RegExp(placeholder);
            var i:int = 0;
            while (placeholderPattern.test(input))
            {
                input = input.replace(placeholderPattern, rest[i]);
                i++;
            }
            return input;
        }
        
        /**
         * 检查字符串是否仅包含空白字符
         */
        public static function isWhitespace(input:String):Boolean
        {
            return trim(input).length == 0;
        }
        
        /**
         * 循环查找文本内所有匹配的字符串
         * @param searchString 目标字符串
         * @param targetString 字符串域
         * @return 字符串域内所有符合规则的索引合集
         */
        public static function findAll(searchString:String, targetString:String):Array
        {
            var indexArr:Array = [];
            var i:int = targetString.indexOf(searchString);
            while (i != -1)
            {
                indexArr.push(i);
                i = targetString.indexOf(searchString, i + searchString.length);
            }
            return indexArr;
            
        }
        
        public static function replaceAll(search:String, replacement:String, area:String):String
        {
            //version 3.0
            area = area.replace(new RegExp(search, "g"), replacement);
            return area;
            
            //version 2.0
            /*var result:String = "";
            var matchFound:Boolean = false;
            var inputLen:Number = area.length;
            var targetLen:Number = replacement.length;
            
            for (var i:Number = 0; i < inputLen; i++)
            {
                if (area.charAt(i) == replacement.charAt(0))
                {
                    matchFound = true;
                    for (var j:Number = 0; j < targetLen; j++)
                    {
                        if (area.charAt(i + j) != replacement.charAt(j))
                        {
                            matchFound = false;
                            break;
                        }
                    }
                    if (matchFound)
                    {
                        result += replacement;
                        i += targetLen - 1;
                        continue;
                    }
                }
                result += area.charAt(i);
            }
            return result;*/
            
            //version1.0
            /*while (area.indexOf(search) != -1)
            {
                area = area.replace(search, replacement);
            }
            return area;*/
        }
        
        /**
         * 获取字符串后若干个字符
         * @param content 字符串
         * @param length 长度
         * @param reverse 是否翻转
         * @return 所需字符集
         */
        public static function getLastMultiChars(content:String, length:int = 1, reverse:Boolean = false):String
        {
            if (length == 0)
            {
                length = content.length;
            }
            var str1:String = content.slice(content.length - length, content.length);
            if (reverse)
            {
                str1 = str1.split("").reverse().join("");
            }
            return str1;
        }
        
        /**
         * 去掉字符串后若干个字符
         * @param content 字符串
         * @param length 长度
         * @return
         */
        public static function trimLastMultiChars(content:String, length:int = 1):String
        {
            return content.slice(0, content.length - length);
        }
        
        /**
         * 给小于10的数字补上0
         * @param num 目标数字
         * @param digit 位数
         * @return 返回字符
         */
        public static function completeZeroFormat(num:uint, digit:int = 2):String
        {
            var numStr:String = num.toString();
            while (numStr.length < digit)
            {
                numStr = "0" + numStr;
            }
            return numStr;
        }
        
        public static function int2StringArray(value:int):Array
        {
            return value.toString().split("");
        }
    }
}
